package org.example.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.example.domain.JsonData;
import org.example.domain.MyLog;
import org.example.domain.User;
import org.example.service.LogService;
import org.example.service.UserService;
import org.example.utils.JwtUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;

/**
 * @author Jimmy
 * @version 1.0
 */
@Aspect
@Component
public class LogAdvice {

    @Autowired
    private UserService userService;
    @Autowired
    private LogService logService;

    @Pointcut("execution(public * org.example.controller.*.*(..))")
    public void controllerPointCut(){}

    @Pointcut("execution(public * org.example.service.*.*(..))")
    public void servicePointCut(){}

//    @Before("servicePointCut()")
//    public  void BeforeController(JoinPoint joinPoint){
//        String name = joinPoint.getSignature().getName();
//        long l = System.currentTimeMillis();
//        System.out.println(l);
//    }

    @AfterReturning(pointcut = "controllerPointCut()",returning = "jsonData")
    public void AfterController(JoinPoint joinPoint, JsonData jsonData){
        long timeMillis = System.currentTimeMillis();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format(timeMillis);
        String name = joinPoint.getSignature().getName();
        String logId = Integer.toString((int) timeMillis);
        HttpServletRequest request = getHttpServletRequest();
        String ip = request.getRemoteAddr();
        StringBuffer requestURL = request.getRequestURL();
        requestURL.append("?operation=").append(name);
        String token = request.getHeader("token");
        String username = null;
        if (token != null){
            String id = JwtUtils.getUserIdFromToken(token);
            User user = userService.findById(id);
            username = user.getUsername();
        }else {
            username = ((JsonData<User>)jsonData).getData().getUsername();
        }
        MyLog log = new MyLog(logId,username,ip,time,name,requestURL.toString());
        int i = logService.addLog(log);

    }


//    @Around("controllerPointCut()")
//    public Object aroundController(ProceedingJoinPoint joinPoint)throws Throwable{
//        HttpServletRequest request = getHttpServletRequest();
//        String token = request.getHeader("token");
//        if (token != null){
//            String id = JwtUtils.getUserIdFromToken(token);
//            User user = userService.findById(id);
//            String username = user.getUsername();
//        }
//
//        String ip = request.getRemoteAddr();
//        return ip;
//    }

    /**
     * 获取request
     * @return
     */
    protected HttpServletRequest getHttpServletRequest(){
        return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
    }

}
